package com.share.im.ui;

import java.util.List;

import org.jivesoftware.smack.XMPPException;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.share.im.R;
import com.share.im.adapter.SearchUserAdapter;
import com.share.im.manager.UserManager;
import com.share.im.manager.ContacterManager.MRosterGroup;
import com.share.im.model.UserBean;

/**
 * 查找用户对话框
 */
public class SearchUserDialog extends Dialog
{

	public SearchUserDialog(Context context)
	{
		super(context);
	}

	public SearchUserDialog(Context context, int theme)
	{
		super(context, theme);
	}

	public static class Builder
	{
		private Context context;
		private View contentView;
		private View rootView; // 缓存view
		private TextView txt_name;
		private ImageView btn_close;
		private Button btn_search;
		private ListView lv_record;
		private List<UserBean> listUser;
		private List<MRosterGroup> rGroups;

		public Builder(Context context,List<MRosterGroup> rGroups)
		{
			this.context = context;
			this.rGroups = rGroups;
		}

		public SearchUserDialog create()
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final SearchUserDialog dialog = new SearchUserDialog(context, R.style.Dialog);
			// 使对话框外界点击无效
			dialog.setCanceledOnTouchOutside(false);

			// 加载布局文件
			if (rootView == null)
				rootView = inflater.inflate(R.layout.dialog_searchuser_layout, null);

			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null)
			{
				parent.removeView(rootView);
			}

			// set the dialog title
			txt_name = (TextView) rootView.findViewById(R.id.txt_name);
			btn_close = (ImageView) rootView.findViewById(R.id.btn_close);
			btn_search = (Button) rootView.findViewById(R.id.btn_search);
			lv_record = (ListView) rootView.findViewById(R.id.lv_record);
			
			btn_close.setOnClickListener(new View.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					dialog.dismiss();
				}
			});
			
			btn_search.setOnClickListener(new View.OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					if(TextUtils.isEmpty(txt_name.getText())){
						txt_name.setError("账户不能为空！");
					}else{
						try
						{
							listUser = UserManager.getInstance(context).searchUsers(txt_name.getText().toString(),rGroups);
							if(listUser==null || listUser.size()<=0){
								lv_record.setAdapter(null);
							}else{
								lv_record.setAdapter(new SearchUserAdapter(context, listUser));
							}
						}
						catch (XMPPException e)
						{
							e.printStackTrace();
						}
					}
				}
			});
			
			lv_record.setOnItemClickListener(new AdapterView.OnItemClickListener()
			{

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					UserBean user = listUser.get(position);
					UserManager.getInstance(context).addUser(user.getUserName(), user.getName());
				}
			});

			dialog.addContentView(rootView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			if (contentView != null)
			{
				// if no message set add the contentView to the dialog body
				((LinearLayout) rootView.findViewById(R.id.content)).removeAllViews();
				((LinearLayout) rootView.findViewById(R.id.content)).addView(contentView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			}
			dialog.setContentView(rootView);
			return dialog;
		}
	}
	
}
